﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AgencyEntityLib.BaseEntity;
using Ecopsy.DataTier;


public partial class Ctl_ctlSurfaceListBrowser : System.Web.UI.UserControl
{

   public  Func<bool> OnSave;
   public Func<bool> OnCancel;

   public Contract Contr { get; set; }

   protected override void OnPreRender(EventArgs e)
   {
       base.OnPreRender(e);
       
       if (null != lvSides.EditItem)
       {
           TextBox t = (TextBox)lvSides.EditItem.FindControl("txtStart");
           RegOnChange(t, "OnChange");


           t = (TextBox)lvSides.EditItem.FindControl("txtEnd");
           RegOnChange(t, "OnChange");

           t = (TextBox)lvSides.EditItem.FindControl("txtConractPrice");
           RegOnChange(t, "OnChange");

           object f = lvSides.EditItem.FindControl("Checkbox1");
           

       }
     
   }

   public string ClientID_DayCount 
   {
       get 
       {
           if (null != lvSides.EditItem)           
               return ((Label)lvSides.EditItem.FindControl("lblDayCount")).ClientID ;
            

           return string.Empty;

       }
   }

   private void RegOnChange(WebControl T, string EventName)
   {
       T.Attributes.Add(EventName, Get_Call_Check_Q_Surface());
   }

   protected  string Get_Call_Check_Q_Surface()
   {
       
       TextBox t = (TextBox)lvSides.EditItem.FindControl("txtStart");
       
       if (null == t)
                    return string.Empty;

       //Сичтать значения
       TextBox t1 = (TextBox)lvSides.EditItem.FindControl("txtEnd");
       TextBox t2 = (TextBox)lvSides.EditItem.FindControl("txtConractPrice");
       Label t3 = (Label)lvSides.EditItem.FindControl("lblDayCount");
       Label t4 = (Label)lvSides.EditItem.FindControl("lblSumma");
       CheckBox t5 = (CheckBox)lvSides.EditItem.FindControl("chqPrioritetLevel");
       HiddenField hfId = (HiddenField)lvSides.EditItem.FindControl("hfId");
              
       ContractSurfaceItem i = (ContractSurfaceItem)ContractSupplier().ContractSurfaceItemList[hfId.Value];
       i.BusinesRulesEngine.OnError = OnError;
       int SfCount = i.GetSide().SurfaceCount;

       string sCmd = "Call_Check_Q_Surface('" + i.ContractID + "','" + t.ClientID + "','" +
                                                    t1.ClientID + "','" + hfId.Value + "','" + t5.ClientID + "'," +
                                                    SfCount + ",'" + t2.ClientID + "','" + t3.ClientID + "','" + t4.ClientID + "');";

       return sCmd;
   }

   private bool OnError(List <string > ErrList)
   {
       return true;
   }

    protected void Page_Load(object sender, EventArgs e)
    {   

    }

    

    private AgencyEntityLib.BaseEntity.ContractSurfaceItemExt.grdContractSurfaceItem[] Source
    {
        get 
        {
            List<AgencyEntityLib.BaseEntity.ContractSurfaceItemExt.grdContractSurfaceItem> l = new List<AgencyEntityLib.BaseEntity.ContractSurfaceItemExt.grdContractSurfaceItem>();
            foreach (ContractSurfaceItem i in ContractSupplier().ContractSurfaceItemList)
            {
                l.Add(new AgencyEntityLib.BaseEntity.ContractSurfaceItemExt.grdContractSurfaceItem(i));
            }

            return l.ToArray<AgencyEntityLib.BaseEntity.ContractSurfaceItemExt.grdContractSurfaceItem>();
        }
    }

    protected override void OnDataBinding(EventArgs e)    
    {
        MyDataBind();
    }

    private void MyDataBind()
    {
        if (ContractSupplier == null) return;

        lvSides.DataSource = Source;
        lvSides.DataBind();
    }

    

    /// <summary>
    /// Функция поставщик поверхностей контракта
    /// </summary>
    protected  Func<Contract> _ContractSupplier;

    public Func<Contract> ContractSupplier { get { return _ContractSupplier; } set { _ContractSupplier = value; } }

        
    protected void lvSides_ItemEditing(object sender, ListViewEditEventArgs e)
    {
        
    }
    protected void lvSides_ItemUpdating(object sender, ListViewUpdateEventArgs e)
    {

        

    }

    /// <summary>
    /// Обновление ContractSurfaceItem
    /// </summary>
    /// <param name="ID"></param>
    /// <param name="editItem"></param>
    /// <returns></returns>
    private bool UpdateRow(string ID, ListViewItem editItem)
    {

        ContractSurfaceItem i = (ContractSurfaceItem)ContractSupplier().ContractSurfaceItemList[ID];
        string s ="";

        s=((TextBox )editItem.FindControl ("txtStart")).Text ;
        i.DtStart = Convert.ToDateTime(s);

        s = ((TextBox)editItem.FindControl("txtEnd")).Text;
        i.DtFinish = Convert.ToDateTime(s);

        s = ((TextBox)editItem.FindControl("txtConractPrice")).Text;
        i.ConractPrice = Convert.ToInt16(s);

        CheckBox c = (CheckBox)editItem.FindControl("chqPrioritetLevel");
        i.qPrioritetLevel =(c.Checked ?1:0);


        Contract cn = (Contract)ContractSupplier();
        //(cn.BusinesRulesEngine as Contract_BusinesRulesEngine).CalculateContractSurfaceItemList_Weight();
        

        return true;
    }

    protected void lvSides_ItemCommand(object sender, ListViewCommandEventArgs e)
    {
        ListView lv = (ListView)sender;
        switch (e.CommandName)
        {
            case "Edit":
                {

                    ListViewDataItem d = (ListViewDataItem)e.Item;
                    lv.EditIndex = d.DataItemIndex;

                    break;
                }
            case "Insert":
                {

                    break;
                }
            case "Update":
                {
                    if (UpdateRow(e.CommandArgument as string, e.Item))
                    {
                        if (null != OnSave)
                                        OnSave();
                        lv.EditIndex = -1;
                    }
                    break;
                }
            case "Delete":
                {
                    
                    break;
                }
            case "Cancel":
                {
                    lv.EditIndex = -1;
                    MyDataBind();
                    if (null != OnCancel) 
                                    OnCancel();
                    return;
                }
        }

        MyDataBind();



    }

    protected string GetMessageCSS(object   Item)
    {
        AgencyEntityLib.BaseEntity.ContractSurfaceItemExt.grdContractSurfaceItem obj = (AgencyEntityLib.BaseEntity.ContractSurfaceItemExt.grdContractSurfaceItem)Item;

        if (obj.In_Q)
                    return "";
        return "hidden";
    }

    protected string GetOkControlID_Q()
    {
        return ctlQ_State1.btnOk_ClientID;
    }

    

    public string GetCancelControlID_Q()
    {
        return ctlQ_State1.BtnCancel_ClientID ;
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        ImageButton b = (ImageButton)sender;
        string s = b.CommandArgument;
        int r = ContractSupplier().ContractSurfaceItemList.Count;

        ContractSupplier().ContractSurfaceItemList.Remove(s);
        
        MyDataBind();

        if (null != OnSave)
            OnSave();
    }
    /// <summary>
    /// Показать очередь 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void ShowQ_Click(object sender, ImageClickEventArgs e)
    {
        ImageButton  btn = ((ImageButton)sender) ;
        ContractSurfaceItem i = (ContractSurfaceItem)ContractSupplier().ContractSurfaceItemList[btn.CommandArgument];

        ctlQ_State1.dtStart = i.DtStart ;
        ctlQ_State1.dtFinish = i.DtFinish ;
        ctlQ_State1.SideId = i.SideID ;
        ctlQ_State1.MyLoad();

        


        ModalPopupext_QList.CancelControlID = ctlQ_State1.BtnCancel_ClientID;
      //  ModalPopupext_QList.OkControlID = ctlQ_State1.btnOk_ClientID;
        ModalPopupext_QList.Show();

    }
    
}
